/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.com.ces4.produccion.client.loging;

import co.com.ces4.produccion.client.AutenticacionService;
import co.com.ces4.produccion.client.AutenticacionServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.BackgroundRepeat;
import com.smartgwt.client.types.TitleOrientation;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Dialog;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.CanvasItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 *
 * @author ias
 */
public class Login extends VLayout {

    private TextItem txtUser;
    private PasswordItem txtPassword;
    private ButtonItem btnSingIn;
    private DynamicForm frmSoftProLoging;
    private CanvasItem LogoContainer;
    private Window modal;

    public Login() {
        setWidth(300);
        setHeight100();
        addStyleName("centerForm");
        setAlign(VerticalAlignment.CENTER);
        frmSoftProLoging = buildDynamicForm();

        LogoContainer = buildImage();
        txtUser = buildTextItem("txtUser", "Usuario");
        txtPassword = buildPasswordItem("txtPassword", "Contraseña");
        btnSingIn = buildButton("btnSingIn", "Ingresar");
        frmSoftProLoging.setItems(LogoContainer, txtUser, txtPassword, btnSingIn);

        KeyPressHandler enterHandler = new KeyPressHandler() {

            public void onKeyPress(com.smartgwt.client.widgets.form.fields.events.KeyPressEvent event) {
                if (event.getKeyName().equals("Enter")) {
                    loging();
                }
            }
        };

        btnSingIn.addClickHandler(new com.smartgwt.client.widgets.form.fields.events.ClickHandler() {

            public void onClick(com.smartgwt.client.widgets.form.fields.events.ClickEvent event) {
                loging();
            }
        });

        txtUser.addKeyPressHandler(enterHandler);
        txtPassword.addKeyPressHandler(enterHandler);


        modal = buildModal("Ingresar al sistema - Softpro");
        modal.addItem(frmSoftProLoging);

        modal.show();

        addMember(modal);

    }

    private DynamicForm buildDynamicForm() {
        DynamicForm frmSoftProLogingS = new DynamicForm();
        frmSoftProLogingS.setWidth100();
        frmSoftProLogingS.setHeight100();
        frmSoftProLogingS.setMargin(5);
        frmSoftProLogingS.setNumCols(2);
        frmSoftProLogingS.setAlign(Alignment.CENTER);
        return frmSoftProLogingS;
    }

    private TextItem buildTextItem(String name, String title) {
        TextItem txtUsuarioS = new TextItem(name, title);
        txtUsuarioS.setTitleOrientation(TitleOrientation.TOP);
        txtUsuarioS.setWrapTitle(false);
        txtUsuarioS.setColSpan(2);
        return txtUsuarioS;
    }

    private PasswordItem buildPasswordItem(String name, String title) {
        PasswordItem txtPasswordS = new PasswordItem(name, title);
        txtPasswordS.setTitleOrientation(TitleOrientation.TOP);
        txtPasswordS.setStartRow(true);
        txtPasswordS.setEndRow(false);
        return txtPasswordS;
    }

    private ButtonItem buildButton(String name, String title) {
        ButtonItem button = new ButtonItem(name, title);
        button.setStartRow(false);
        button.setLeft(30);
        return button;
    }

    private CanvasItem buildImage() {
        CanvasItem imageFrame = new CanvasItem();
        imageFrame.setShowTitle(false);
        imageFrame.setWidth(270);
        imageFrame.setHeight(91);
        imageFrame.setColSpan(2);

        Canvas imageS = new Canvas();
        imageS.setBackgroundImage(GWT.getModuleBaseURL() + "logoP.png");
        imageS.setBackgroundRepeat(BackgroundRepeat.NO_REPEAT);
        imageS.setWidth100();
        imageS.setHeight100();
        imageFrame.setCanvas(imageS);
        return imageFrame;
    }

    private Window buildModal(String title) {
        Window modalS = new Window();
        modalS.setTitle(title);
        modalS.setWidth(300);
        modalS.setHeight(250);

        modalS.setShowMinimizeButton(false);
        modalS.setIsModal(true);
        modalS.setShowModalMask(true);
        modalS.setCanDrag(false);
        modalS.setCanDragResize(false);
        modalS.setCanDrop(false);
        modalS.setCanAcceptDrop(false);
        modalS.setCanDragReposition(false);
        modalS.setCanDragScroll(false);
        modalS.centerInPage();
        return modalS;
    }

    private void loging() {
        getAutenticationServices().validateUser(txtUser.getValueAsString(), txtPassword.getValueAsString(), new AsyncCallback<Boolean>() {

            public void onFailure(Throwable caught) {
                SC.warn("Error", caught.getMessage(), new BooleanCallback() {

                    @Override
                    public void execute(Boolean value) {
                    }
                }, new Dialog());
            }

            public void onSuccess(Boolean result) {
                if (result) {
                    RootPanel.get().remove(Login.this);
                    PrincipalMenu ppalMenu = new PrincipalMenu();
                } else {
                    SC.warn("Error", "Usuario o contraseña inválido", new BooleanCallback() {

                        @Override
                        public void execute(Boolean value) {
                        }
                    }, new Dialog());
                    frmSoftProLoging.clearValues();
                }
            }
        });
    }

    public static AutenticacionServiceAsync getAutenticationServices() {
        return GWT.create(AutenticacionService.class);
    }
}
